<?php
namespace Admin\Model;
use Think\Model;

class WeekreportModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE)
    );
	
	
	protected $_auto = array(
    	array('UserID','getUserID',1,'callback')
  	);
    
    
    public function getUserID(){
		return session('USER_ID');
	}
	
	public function Myupadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$where = "";
		$inid = "";
		$keyword = I('keyword');
        if(!empty($keyword)){
			$res = M("project")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				$where['ProjectID'] = array('IN',substr($inid, 0, -1));
			}else{
				$where['ProjectID'] = "-1";
			}
		}
		$return = array();
		$count = $this->where($where)->count();
	    $page = $this->Mypage($count, 8);
	    $fields = " a.*, b.Name AS ProjectName ";
	    $join = " LEFT JOIN project AS b ON a.ProjectID = b.ID ";
		$res = $this->alias('a')->join($join)->where($where)->field($fields)->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
	public function getTableData(){
		$dataArr = array();
		$member = " ";
		$j = 0;
		$join1 = " LEFT JOIN projectcommunication AS b ON a.ID = b.WPID ";
		$res1 = $this->alias('a')->field("b.ID AS commID,a.*,a.Content AS WeekContent,b.Content AS ComContent,b.ComTime,b.CustomMember,b.ProjectMember,b.OutputContent,b.Memo")->join($join1)->where(array("ProjectID"=>$_GET['id']))->select();

		foreach($res1 AS $k => $v){  //多条沟通
			$K = $k - 1;
			if($res1[$k]['ID'] != $res1[$K]['ID']){
				$j++;
			}
			$res1[$k]['NEWID'] = $j;
		}
		
		foreach($res1 AS $k => $v){
			 $res1[$k]['cycle'] = "第".$this->large($v['NEWID'])."周";
			 $res1[$k]['cycleDate'] = $v['StartDate']." ~ ".$v['EndDate'];
		}
		$join2 =  " LEFT JOIN user AS b ON a.ChargeUserID = b.ID";
		$res2 = M("project")->alias('a')->field("a.Name,a.ImportantPoint,a.StartDate,a.EndDate,b.UserName")->join($join2)->where(array("a.ID"=>$_GET['id']))->find();
		if(!empty($res2['StartDate']) && !empty($res2['EndDate'])){
			$strtotime = (strtotime($res2['EndDate']) - strtotime($res2['StartDate']));
			$projectCycle = ((int)$strtotime / (60*60*24)) >= 0 ? (int)$strtotime / (60*60*24) : 0;
		}
		$res3 = M("project_user_rel as a")->field("b.UserName")->join("LEFT JOIN user AS b ON a.UserID = b.ID")->where(array("a.ProjectID"=>$_GET['id']))->select();
		if(!empty($res3)){
			foreach($res3 AS $k => $v){
				$member .= $v['UserName'].',';
			}
		}
		$dataArr['projectName'] = $res2['Name'];
		$dataArr['projectCycle'] = $projectCycle ? $projectCycle : "";
		$dataArr['ChargeUserName'] = $res2['UserName'];
		$dataArr['projectMember'] = substr($member,0,-1);
		$dataArr['ImportantPoint'] = $res2['ImportantPoint'];
		$dataArr['data'] = $res1;
		return $dataArr;
	}
	
	
	public function large($number){
		$number = substr($number,0,2);
		$arr = array("零","一","二","三","四","五","六","七","八","九");
	 	if(strlen($number) == 1){
	  		$result = $arr[$number];
	  	}else{
	   		if($number == 10){
	    		$result = "十";
	   		}else{
	    		if($number < 20){
	    			$result = "十";
	    		}else{
	    			$result = $arr[substr($number,0,1)]."十";
	    		}
	    		if(substr($number,1,1) != "0"){
	    			$result .= $arr[substr($number,1,1)]; 
	    		}
	  		}
	 	}
		return $result;
	}
	
	
	public function setTableData(){
		$in = I();
		$weekData = $in['week'];
		$commData = $in['comm'];
		$allDate = explode(" ~ ",$weekData['allDate']);
		unset($weekData['allDate']);
		$weekData['StartDate'] = $allDate[0];
		$weekData['EndDate'] = $allDate[1];
		$resid = $this->add($weekData);
		if($resid){
			$commData['WPID'] = $resid;
			$commDB = M("projectcommunication");
			$res = $commDB->add($commData);
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "添加错误！";
		}
		return $this->results;
	}
	
	
	public function updateTableDataW(){
		$in = I();
		if($in['allDate']){
			$allDate = explode(" ~ ",$in['allDate']);
			$weekData['StartDate'] = $allDate[0];
			$weekData['EndDate'] = $allDate[1];
		}else{
			$weekData = $in;
			unset($weekData['ID']);
		}
		$res = $this->where(array("ID" => $in['ID']))->save($weekData); 
		if($res) {
			$this->results['state'] = 1;
			$this->results['msg'] = "修改成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "修改错误！";
		}
		return $this->results;
	}
	
	
	public function updateTableDataC(){
		$in = I();
		$coomData = $in;
		$commDB = M("projectcommunication");
		unset($coomData['ID']);
		$res = $commDB->where(array("ID" => $in['ID']))->save($coomData); 
		if($res) {
			$this->results['state'] = 1;
			$this->results['msg'] = "修改成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "修改错误！";
		}
		return $this->results;
	}
	
	
}

?>				